import {computed, reactive, ref} from "vue";
import {FetchZZYClassListDAT} from "../../types/responseTypes";
import {getZZYClassList} from "../../api/classes";
import {useStore} from "vuex";

export function useZZYClassList(){

    const total = ref(0)
    const classListWrap = reactive<{
        ZZYClassList: FetchZZYClassListDAT[]
    }>({
        ZZYClassList: []
    })

    const {state} = useStore()

    function initZZYClassList(page: number = 1, pageSize: number = 20){
        return new Promise((resolve, reject) => {
            getZZYClassList({
                PAGESIZE: pageSize,
                PAGEINDEX: page,
                OPERATOR_ID: state.user.userInfo.CUSER_ID
            }).then(res => {
                if (res.ERROR[0].errcode == '0'){
                    total.value = res.COUNT[0].NUM
                    if (res.DAT.length === 0){
                        reject('nomore')
                    }

                    classListWrap.ZZYClassList.push(...res.DAT)
                    resolve(null)

                }else {
                    uni.showToast({
                        title: res.ERROR[0].error,
                        icon: 'none',
                        mask: true
                    })
                    reject('err')
                }
            }).catch(err => {
                uni.showToast({
                    title: '获取列表失败',
                    icon: 'error',
                    mask: true
                })
                reject('err')
            })
        })

    }

    return {
        total,
        ZZYClassList: computed(() => classListWrap.ZZYClassList),
        initZZYClassList
    }
}
